package com.zx.dsptuneup_cloud.activity;

import static com.zx.dsptuneup_cloud.App.getContext;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

import com.google.android.material.tabs.TabLayout;
import com.gyf.barlibrary.ImmersionBar;
import com.orhanobut.hawk.Hawk;
import com.zx.dsptuneup_cloud.Adapter.MultiFragmentPagerAdapter;
import com.zx.dsptuneup_cloud.App;
import com.zx.dsptuneup_cloud.Base.BaseActivity;
import com.zx.dsptuneup_cloud.Base.BaseFragment;
import com.zx.dsptuneup_cloud.Base.NoScrollViewPager;
import com.zx.dsptuneup_cloud.R;
import com.zx.dsptuneup_cloud.Tools.Constant;
import com.zx.dsptuneup_cloud.Util.Constants;
import com.zx.dsptuneup_cloud.Util.InterceptionStringUtil;
import com.zx.dsptuneup_cloud.Util.Netty.NettyListener;
import com.zx.dsptuneup_cloud.fragment.AudioMatrixFragment;
import com.zx.dsptuneup_cloud.fragment.InputFragment;
import com.zx.dsptuneup_cloud.fragment.OutPutFragment;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;

public class TestNetActivity extends BaseActivity implements NettyListener {

    @BindView(R.id.start1)
    TextView start1;

    @BindView(R.id.start2)
    TextView start2;

    @BindView(R.id.num_send)
    TextView num_send;

    @BindView(R.id.num_recive)
    TextView num_recive;

    @BindView(R.id.clear)
    TextView clear;
    @BindView(R.id.back)
    TextView back;
    @BindView(R.id.stop)
    TextView stop;

    private Handler handler;
    private Runnable runnable1;
    private Runnable runnable2;
    private Runnable runnable3;
    private Runnable runnable4;
    private Runnable runnable5;
    private Runnable runnable9;

    private int sendNum = 0;//14输入通道包数量标识
    private int reciveNum = 0;//8通道输出数量标识
    private int delayedNum = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_net);
        initUI();
        App.getContext().getNettyClient().setListener(TestNetActivity.this);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();

            }
        });
      //  setQuery();  // 查询指令
        start1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setQuery();
      /*          runnable5 = new Runnable() {
                    @Override
                    public void run() {
                        if (sendNum>1000){
                            return;
                        }
                        App.getContext().getProtocolServiceUtil().selectEquipmentNumbe();
                        handler.postDelayed(this, 200);
                        handler.postDelayed(runnable5, 80);
                        sendNum++;
                        num_send.setText("发送次数："+sendNum+"");
                        Log.d("grtre","yuiyyyyyyyyyyt");
                    }
                };*/
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (runnable5!=null){
                    handler.removeCallbacks(runnable5);
                }

            }
        });
        start2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                App.getContext().getProtocolServiceUtil().selectEquipmentNumbe();
            }
        });
     clear.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (runnable5!=null){
            handler.removeCallbacks(runnable5);
        }
        num_send.setText("发送次数："+0);
        sendNum=0;
        reciveNum=0;
        num_recive.setText("接收次数："+0);
       }
    });
 /*       // 初始化发送指令
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Init.initData1();
                }catch (Exception e){

                }
            }
        }).start();*/

    }


    protected void initUI() {


    }

    /**
     * 查询状态指令
     */
    private void setQuery() {
        handler = new Handler();
        runnable5 = new Runnable() {
            @Override
            public void run() {
                if (sendNum>1000){
                    return;
                }
                App.getContext().getProtocolServiceUtil().selectEquipmentNumbe();
                handler.postDelayed(this, 200);
                sendNum++;
                num_send.setText("发送次数："+sendNum+"");
                Log.d("grtre","yuiyyyyyyyyyyt");
            }
        };
        handler.postDelayed(runnable5, 100);


    }

    private void initTabListener() {

    }

    @Override
    public void onMessageReaderTimeout() {

    }

    /**
     * 回调客户端接收的信息
     * I/System.out: 客户端开始读取服务端过来的信息
     * 2e637669 0632 0000 15a1 0400 00c0008d00000000e0008d0000000080008d00000000a0008d0000000040018d0000000060018d0000000100000040008d0000000060008d0000000000008d0000000020008d00000000c0008d00000000e0008d0000000080008d00000000a0008d0000000040018d0000000060018d0000000100000040008d0000000060008d0000000000008d0000000020008d00000000c0008d00000000e0008d0000000080008d00000000a0008d0000000040018d0000000060018d0000000100000040008d0000000060008d0000000000008d0000000020008d00000000c0008d00000000e0008d58020000a00f000000000000008d0000000020008d00000000c0008d00000000e0008d0000000080008d00000000a0008d0000000040018d0000000060018d00000000e0008d00000000e0008d00000000e0008d00000000e0008d00000000e0008d000000010000204e1400000040008d0000000060008d0000000000008d0000000020008d00000000c0008d00000000e0008d0000000080008d00000000a0008d0000000040018d0000000060018d00000000e0008d00000000e0008d00000000e0008d00000000e0008d00000000e0008d000000010000204e1400000040008d0000000060008d0000000000008d0000000020008d00000000c0008d00000000e0008d0000000080008d00000000a0008d0000000040018d0000000060018d00000000e0008d00000000e0008d00000000e0008d00000000e0008d00000000e0008d000000010000204e1400000040008d0000000060008d0000000000008d0000000020008d00000000c0008d00000000e0008d0000000080008d00000000a0008d0000000040018d0000000060018d00000000e0008d00000000e0008d00000000e0008d00000000e0008d00000000e0008d000000010000204e1400000040008d0000000060008d0000000000008d0000000020008d00000000c0008d00000000e0008d0000000080008d00000000a0008d0000000040018d0000000060018d00000000e0008d00000000e0008d00000000e0008d00000000e0008d00000000e0008d000000010000204e1400000040008d0000000060008d0000000000008d0000000020008d00000000c0008d00000000e0008d0000000080008d00000000a0008d0000000040018d0000000060018d00000000e0008d00000000e0008d00000000e0008d00000000e0008d00000000e0008d000000010000204e1400000040008d0000000060008d0000000000008d0000000020008d00000000c0008d00000000e0008d0000000080008d00000000a0008d0000000040018d0000000060018d00000000e0008d00000000e0008d00000000e0008d00000000e0008d00000000e0008d000000010000204e1400000040008d0000000060008d0000000000008d0000000020008d00000000c0008d00000000e0008d0000000080008d00000000a0008d0000000040018d0000000060018d00000000e0008d00000000e0008d00000000e0008d00000000e0008d00000000e0008d000000010000204e1400000040008d0000000060008d0000000000008d0000000020008d00000000c0008d00000000e0008d0000000080008d00000000a0008d0000000040018d0000000060018d00000000e0008d00000000e0008d00000000e0008d00000000e0008d00000000e0008d000000010000204e1400000040008d0000000060008d0000000000008d0000000020008d00000000c0008d00000000e0008d0000000080008d00000000a0008d0000000040018d0000000060018d00000000e0008d00000000e0008d00000000e0008d00000000e0008d00000000e0008d000000010000204e1400000040008d0000000060008d0000000000008d0000000020008d00000000c0008d00000000e0008d0000000080008d00000000a0008d0000000040018d0000000060018d00000000e0008d00000000e0008d00000000e0008d00000000e0008d00000000e0008d000000010000204e1400000040008d0000000060008d0000000000008d0000000020008d00000000c0008d00000000e0008d0000000080008d00000000a0008d0000000040018d0000000060018d00000000e0008d00000000e0008d00000000e0008d00000000e0008d00000000e0008d000000010000204e140000000000002e646e0d
     */
    @Override
    public void onMessageResponse(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //  客户端开始读取服务端过来的信息
                //  StringUtil.dealWithMsgReceive(msg);
                if (msg.length() < 28) {
                    return;
                }
                String order1 = msg.substring(24, 28);//指令
              //  Log.d(TAG + "kkkk", order1 + "  长度" + msg.length() + "  数据=" + msg + "");
                switch (order1) {

                    case "4103":
                        InterceptionStringUtil.getData8Channel(msg);


                        break;
                    case "4105":
                        if ((msg.length() == 804)) {


                        }
                        if ("656e642e".equals(msg.substring(msg.length() - 8, msg.length()))) ;
                    {
                        Intent intent = new Intent(Constants.RESH_RECEIVER_BROADCAST);
                        sendBroadcast(intent);
                        Hawk.put("Matrix", msg);
                        Log.d("Matrixhfgf", msg);
                    }
                    // 查询14通道数据
                    App.getContext().getProtocolServiceUtil().selectData14Channel();
                    break;
                    case "4107":
                        break;
                    case "4109":
                        InterceptionStringUtil.setDataRemote(msg);
                        //查询心跳  获取设备型号、版本号
                        App.getContext().getProtocolServiceUtil().selectHeartBeat();
                        break;
                    case "410b":
                        InterceptionStringUtil.getDataInputParame(msg);
                      /*  //回复输入通道设置参数
                        Log.d(TAG,msg+"通道="+ByteToString.strTo10(msg.substring(32,34))+" 事件="+msg.substring(34,36)+
                                " 几段="+ByteToString.strTo10(msg.substring(36,38))+" type="+msg.substring(38,40)+"  频率="+ByteToString.strTo10(msg.substring(40,44))+
                                " q值="+ NumberBaseConversionUtil.SixToDecimalTen(msg.substring(44,48))+"  增益="+NumberBaseConversionUtil.SixToDecimalTen(msg.substring(48,52)));*/
                        break;
                    case "410d":
                        InterceptionStringUtil.getDataInputParame(msg);
                        break;
                    case "410f":
                        InterceptionStringUtil.getDataMatrix(msg);
                        //   Toast.makeText(HomeActivity.this,"矩阵数据数据加载完成",Toast.LENGTH_LONG).show();
                        break;
                    case "0002":
                        InterceptionStringUtil.getHeartBeat(msg);
                        break;

                    case "7706":
                        String num = msg.substring(32, 36);
                        reciveNum++;
                        num_recive.setText("接收次数："+reciveNum);
                        break;
                    case "4113":
                        InterceptionStringUtil.getDataRemote(msg);
                        break;

                }
//
            }
        });
    }

    //连接状态变化的时候 会走这
    @Override
    public void onServiceStatusConnectChanged(final int statusCode) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (statusCode == NettyListener.STATUS_CONNECT_SUCCESS) {
                   // Log.e(TAG, "STATUS_CONNECT_SUCCESS:");
                    if (App.getContext().getNettyClient().getConnectStatus()) {
                    }
                } else {
                   // Log.e(TAG, "onServiceStatusConnectChanged:" + statusCode);
                    // 连接断开
                    Toast.makeText(TestNetActivity.this, "连接断开", Toast.LENGTH_LONG).show();
                    //  App.getContext().getNettyClient().disconnect();
                    // CommonAction.getInstance().OutSign();
                    App.getContext().getNettyClient().reconnect();
                    // startActivity(new Intent(HomeActivity.this, LoginActivity.class));
                }
            }
        });
    }


    private void setSelectTextBoldAndBig(TabLayout.Tab tab) {
        TextView textView = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.design_layout_tab_text, null);
        textView.setText(tab.getText());
        textView.setScaleY(1.1f);
        textView.setScaleX(1.1f);
//        textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        textView.setTextColor(Color.parseColor("#0A0A0A"));
        tab.setCustomView(textView);
    }

    @Override
    public void onBackPressed() {

    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e("onPause", "onPause called");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();


        Log.e("onDestroy", "onDestroy called");
    }

    private class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            delayedNum = 58;

        }
    }
}
